package com.laboratory.thread;

import com.laboratory.util.DateUtil;
import org.apache.commons.lang.time.DateUtils;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class CompletableTest {


    private static final ExecutorService executorService = new ThreadPoolExecutor(1, 10, 60L,
            TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>(1024), new ThreadFactory() {
        private int i = 0;

        @Override
        public Thread newThread(Runnable r) {
            i++;
            Thread thread = new Thread(r, "TaskEngine--Pool-" + this.i);
            return thread;
        }
    });

    public static void main(String[] args) throws Exception{

        CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(DateUtil.currentDateTime()+"begin ======>>>>>>>>> ");
            try {
                System.out.println(DateUtil.currentDateTime()+" do step 1..." );
                Thread.sleep(120*1000);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println(DateUtil.currentDateTime()+"异常==========>>>>>>>> " );

            }finally {
                System.out.println(DateUtil.currentDateTime()+" step 1 finished");
            }


             try {
                System.out.println(DateUtil.currentDateTime()+" do step 2..." );
                Thread.sleep(60*1000);
            }catch (Exception e){
                e.printStackTrace();
                System.out.println(DateUtil.currentDateTime()+"异常==========>>>>>>>> " );

            }finally {
                System.out.println(DateUtil.currentDateTime()+" step 2 finished");
            }
            return true;
        }, executorService);

        Boolean aBoolean = future.get();
        System.out.println("aBoolean = " + aBoolean);

    }


}
